<?php

/**
 *  +----------------------------------------------------------------------
 *  | 文件功能：ReportController.php：
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2022~2025 https://www.huihuiv.com All rights reserved. 
 *  +----------------------------------------------------------------------
 *  | Create At: 2025年05月14日 00:14:41
 *  +----------------------------------------------------------------------
 *  | Author: 半壶水 <star@cqshanwen.com>
 *  +----------------------------------------------------------------------
 */

namespace App\Http\Controllers\Api;

use App\Models\CardModel;
use App\Models\PayRecordModel;
use App\Models\TopUpRecordModel;
use App\Utils\Message;
use Illuminate\Http\Request;

class ReportController extends BaseController
{
    /**
     * zhifubaobiao
     *
     * @param Request $request
     * @param PayRecordModel $payRecordModel
     * @return void
     */
    public function get_pay_report(
        Request $request,
        PayRecordModel $payRecordModel
    ) {
        $page = $request->input('page', 1);
        $size = $request->input('size', 10);
        $start = ($page - 1) * $size;
        $startTime = $request->input('start_time', '');
        $endTime = $request->input('end_time', '');
        $phone = $request->input('phone', '');
        $status = $request->input('status', '');
        $query = $payRecordModel::query();
        if (!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        if (!empty($phone)) {
            $query->where(['phone' => $phone]);
        }
        if (!empty($status)) {
            $query->where(['status' => $status]);
        }
        $phoneList = PayRecordModel::pluck('phone')->unique()->map(fn($item) => [
            'value' => $item,
            'label' => $item
        ])->values();

        $count = $query->count();
        $list = $query->orderBy('record_id', 'desc')->offset($start)->limit($size)->get();

        return Message::show_message(true, 'success', [
            'phone_list' => $phoneList,
            'list' => $list,
            'total' => $count,
            'page' => $page,
            'size' => $size
        ]);
    }

    /**
     * 用户报表
     *
     * @param Request $request
     * @param CardModel $cardModel
     * @return void
     */
    public function get_card_report(
        Request $request,
        CardModel $cardModel
    ) {
        $page = $request->input('page', 1);
        $size = $request->input('size', 10);
        $start = ($page - 1) * $size;
        $startTime = $request->input('start_time', '');
        $endTime = $request->input('end_time', '');
        $phone = $request->input('phone', '');
        $status = $request->input('status', '');
        $query = $cardModel::query();
        if (!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        if (!empty($phone)) {
            $query->where(['phone' => $phone]);
        }
        if (!empty($status)) {
            $query->where(['status' => $status]);
        }
        $phoneList = CardModel::pluck('phone')->unique()->map(fn($item) => [
            'value' => $item,
            'label' => $item
        ])->values();

        $count = $query->count();
        $list = $query->orderBy('record_id', 'desc')->offset($start)->limit($size)->get();

        return Message::show_message(true, 'success', [
            'phone_list' => $phoneList,
            'list' => $list,
            'total' => $count,
            'page' => $page,
            'size' => $size
        ]);
    }

    public function get_topup_report(
        Request $request,
        TopUpRecordModel $topUpRecordModel
    ) {
        $page = $request->input('page', 1);
        $size = $request->input('size', 10);
        $start = ($page - 1) * $size;
        $startTime = $request->input('start_time', '');
        $endTime = $request->input('end_time', '');
        $phone = $request->input('phone', '');
        $status = $request->input('status', '');
        $query = $topUpRecordModel::query();
        if (!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        if (!empty($phone)) {
            $query->where(['mobile' => $phone]);
        }
        if (!empty($status)) {
            $query->where(['status' => $status]);
        }
        $phoneList = TopUpRecordModel::pluck('mobile')->unique()->map(fn($item) => [
            'value' => $item,
            'label' => $item
        ])->values();

        $count = $query->count();
        $list = $query->orderBy('record_id', 'desc')->offset($start)->limit($size)->get();

        return Message::show_message(true, 'success', [
            'phone_list' => $phoneList,
            'list' => $list,
            'total' => $count,
            'page' => $page,
            'size' => $size
        ]);
    }
}
